package com.aaa.service.impl;

import com.aaa.entity.MyPage;
import com.aaa.entity.User;
import com.aaa.mapper.UserMapper;
import com.aaa.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 添加一条用户数据
     * @return
     */
    @Override
    public Boolean addUser(User user) {
        int i=-1;
        //获取输入id
        Integer id = user.getId();
        //如果id不为空  执行修改的操作
        //如果id等于空 执行添加的操作业务
        if(id==null){
            //获取密码
            String password = user.getPassword();
            //加盐
            String salt = UUID.randomUUID().toString();
            user.setSalt(salt);
            //密文
            String s = DigestUtils.md5DigestAsHex((password+ salt).getBytes());
            user.setPassword(s);
            //时间
            user.setCreatetime(new Date());
            i = userMapper.insert(user);
        }else{
            user.setUpdatetime(new Date());
            i = userMapper.updateByPrimaryKeySelective(user);
        }
        return i>0?true:false;
    }

    /**
     * 查询该用户是否存在的业务逻辑
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        /**
         * 根据名字查询盐
         */
        //1,根据名字  查询用户信息
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("username",user.getUsername());
        //追加条件  状态为启用状态
        example.and().andEqualTo("status",1);
        User user1 = userMapper.selectOneByExample(example);

        if(user1!=null){
            //获取该用户的盐
            String salt = user1.getSalt();
            //获取输入密码的密文
            String inputpassword = user.getPassword();
            String saltpassword = DigestUtils.md5DigestAsHex((inputpassword + salt).getBytes());
            //获取该用户名的密文
            String userpassword = user1.getPassword();
            if(saltpassword.equals(userpassword)){
                return user1;
            }
        }
        return null;
    }
    /**
     * 查询全部用户信息的业务
     * @return
     */
    @Override
    public PageInfo findAll(MyPage myPage,User user) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        List<User> allUser = userMapper.findAllUser(user);
        PageInfo pageInfo = new PageInfo(allUser);
        return pageInfo;
    }

    /**
     * 修改用户的状态业务
     * @param user
     * @return
     */
    @Override
    public Boolean updatestatus(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        return  i>0?true:false;
    }

    /**
     * 检测用户名是否存在业务
     * @param username
     * @return
     */
    @Override
    public Boolean checkName(String username) {
        User user = new User();
        user.setUsername(username);
        User user1 = userMapper.selectOne(user);
        if(user1!=null){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 查询登录用户的全部信息
     * @param user
     * @return
     */
    @Override
    public User showUserByName(User user) {
        User user1 = userMapper.findUser(user.getUsername());
        return user1;
    }

    /**
     * 修改用户的密码
     * @param user
     * @return
     */
    @Override
    public Boolean updpassword(User user) {
        //获取密码
        String password = user.getPassword();
        //加盐
        String salt = UUID.randomUUID().toString();
        user.setSalt(salt);
        //密文
        String s = DigestUtils.md5DigestAsHex((password+ salt).getBytes());
        user.setPassword(s);
        //时间
        user.setUpdatetime(new Date());

        int i = userMapper.updateByPrimaryKeySelective(user);
        return  i>0?true:false;
    }
}
